//
//  YouXueAndLunTanViewController.m
//  MagazineShangejie
//
//  Created by wen on 16/2/24.
//  Copyright © 2016年 CnbizMedia. All rights reserved.
//

#import "YouXueAndLunTanViewController.h"
#import "Util.h"
@interface YouXueAndLunTanViewController (){

}

@property (weak, nonatomic) IBOutlet UITextField *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *manNumberLabel;


@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UILabel *placeLabel;
@property (weak, nonatomic) IBOutlet UILabel *noteLabel;



@end

@implementation YouXueAndLunTanViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@"报名确定后，请安排好自行出发时间到达发团地点汇合逾期不侯，请按时到达。"];
    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@"请安排好自行出发时间到达发团地点汇合逾期不侯，"].location, [[noteStr string] rangeOfString:@"请安排好自行出发时间到达发团地点汇合逾期不侯，"].length);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:redRange];
    
    NSRange redRangeTwo = NSMakeRange([[noteStr string] rangeOfString:@"同意"].location, [[noteStr string] rangeOfString:@"同意"].length);
//    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:redRangeTwo];
    
    NSDictionary *titleDic = [UserDefaults objectForKey: @"titleDic"];
    NSLog(@"titleDic ====== %@",titleDic);
    _titleLabel.text = [titleDic objectForKey:@"title"];
    _placeLabel.text  = [NSString stringWithFormat:@"发团地点:%@ %@",[titleDic objectForKey:@"area"],[titleDic objectForKey:@"address"] ];
    _timeLabel.text  =  [NSString stringWithFormat:@"发团时间:%@",[titleDic objectForKey:@"activitytime"] ];
    
    [_noteLabel setAttributedText:noteStr];
    [_noteLabel sizeToFit];
    
    
    
    
    //2. 取出数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSInteger number = [userDefault integerForKey:@"buttonTagNumber"];
    
    
    switch (number) {
        case 0:
            //论坛报名
            
            
            self.title = @"活动报名";
        
            break;
            
        case 1:
            
           
            self.title = @"游学报名";
            break;
            
        case 2:
            //            咨询
            self.title = @"咨询";
            break;
            
        case 3:
            
            
            self.title = @"创投报名";
            break;
            
        default:
            break;
    }
    

}

- (IBAction)upManNumber:(id)sender {
    _manNumberLabel.text = [NSString stringWithFormat:@"%ld",[_manNumberLabel.text integerValue] + 1];
}

- (IBAction)downManNumber:(id)sender {
    if ([_manNumberLabel.text integerValue] == 0) {
        return;
    }
    
    else {
        _manNumberLabel.text = [NSString stringWithFormat:@"%ld",[_manNumberLabel.text integerValue] - 1];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [_phoneLabel resignFirstResponder];
    [_nameLabel resignFirstResponder];
}


- (IBAction)submitButton:(id)sender {
    
    //2. 取出数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *lunTanDic = [userDefault objectForKey:@"lunTanDic"];
    
    
//    //创投报名
//    if (number == 0) {
//        
//        ///意见建议
//        [SVProgressHUD show];
//        if (self.cooperationTextView.text.length>1 && _nameLabel.text.length > 1 && _phoneLabel.text.length !=11) {
//            NSString *idString = [lunTanDic objectForKey:@"id"];
//            NSString *catid    = [lunTanDic objectForKey:@"catid"];
//            
//            [[AppAPIClient sharedClient] POST:@"Apply/Add" parameters:@{@"id":idString,@"catid":catid,@"post_name":_nameLabel.text,@"post_mobile":_phoneLabel.text,} success:^(AFHTTPRequestOperation *operation, id responseObject) {
//                //                int code = [responseObject[@"code"] intValue];
//                [SVProgressHUD showSuccessWithStatus:@"提交成功"];
//                
//                [self.navigationController popViewControllerAnimated:YES];
//            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//                [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
//                NSLog(@"Error:%@ => %@",error,operation.responseString);
//            }];
//        }else{
//            [SVProgressHUD showErrorWithStatus:@"请填写完资料,并确保信息准确..."];
//        }
//        
//    }
    
    //活动，游学报名
//    else{
    
        [SVProgressHUD show];
        if (_nameLabel.text.length > 1 && _phoneLabel.text.length ==11) {
            
            NSString *idString = [lunTanDic objectForKey:@"id"];
            NSString *catid    = [lunTanDic objectForKey:@"catid"];
            
            [[AppAPIClient sharedClient] POST:@"Apply/Add" parameters:@{@"id":idString,@"catid":catid,@"post_name":_nameLabel.text,@"post_mobile":_phoneLabel.text,@"post_num":_manNumberLabel.text,} success:^(AFHTTPRequestOperation *operation, id responseObject) {
                //                int code = [responseObject[@"code"] intValue];
                [SVProgressHUD showSuccessWithStatus:@"提交成功"];
                
                [self.navigationController popViewControllerAnimated:YES];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [SVProgressHUD showErrorWithStatus:LOADING_TEXT_ERROR];
                NSLog(@"Error:%@ => %@",error,operation.responseString);
            }];
        }
        else{
            [SVProgressHUD showErrorWithStatus:@"请填写完资料,并确保信息准确..."];
        }
//    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
